(function($, owner) {
	owner.createState = function(user, callback) {
		var state = owner.getState();
		state.user = user;
		state.enginnerId = user.enginnerId;
		state.name = user.name;
		state.token = user.token;
		state.tokenTime = new Date().getTime() + '';
		state.locationReportingInterval = user.locationReportingInterval || 300000;
		owner.setState(state);
		return callback();
	};

	/**
	 * 获取当前状态
	 **/
	owner.getState = function() {
		var stateText = h.getItem('$state') || "{}";
		return JSON.parse(stateText);
	};

	owner.getUser = function() {
		var state = owner.getState()
		if (!state.user) {
			return {};
		}
		return state.user;
	}

	/**
	 * 设置当前状态
	 **/
	owner.setState = function(state) {
		state = state || {};
		h.setItem('$state', JSON.stringify(state));
		//var settings = owner.getSettings();
		//settings.gestures = '';
		//owner.setSettings(settings);
	};

	var checkEmail = function(email) {
		email = email || '';
		return (email.length > 3 && email.indexOf('@') > -1);
	};

	/**
	 * 获取应用本地配置
	 **/
	owner.setSettings = function(settings) {
		settings = settings || {};
		h.setItem('$settings', JSON.stringify(settings));
	}

	/**
	 * 设置应用本地配置
	 **/
	owner.getSettings = function() {
		var settingsText = h.getItem('$settings') || "{}";
		return JSON.parse(settingsText);
	}

	owner.jumpTo = function(url, id, extras) {
		$.openWindow({
			url: url,
			id: id,
			waiting: {
				autoShow: false, //自动显示等待框，默认为true
				title: '正在加载...', //等待对话框上显示的提示内容  正在加载
			},
			extras: extras
		})
	}

	owner.backLogin = function() {
		var state = owner.getState()
		state.user = {};
		state.enginnerId = '';
		state.name = '';
		state.token = '';
		state.tokenTime = '';
		owner.setState(state);
		if (plus.os.name == 'iOS') {
			try{
				window.plus.Push.clearAllLocalNotificationsIniOS();
				window.plus.Push.resetBadge();
				window.plus.Push.setApplicationIconBadgeNumber(0);
			}catch(e){

			}
		}
		stopUpdatingLocation();
		var launch = plus.webview.getLaunchWebview();
		launch.show();
		// var main = plus.webview.getSecondWebview();
		// plus.webview.hide(main)
		$.fire(launch, 'closeOthersWebview', null);
	}

	/**
	 * toast提示
	 * 
	 * @param {String} tip
	 */
	owner.toast = function(tip) {
		var title = tip || '请求失败, 请联系系统管理员'
		$.toast(title);
	}

	/**
	 * 显示加载框
	 * 
	 * @param {String} tip
	 */
	owner.showWaiting = function(tip) {
		var title = tip || '加载中...'
		plus.nativeUI.showWaiting(title)
	}

	/**
	 * 关闭加载框
	 */
	owner.closeWaiting = function() {
		plus.nativeUI.closeWaiting();
	}

	/**
	 * 获取当前网络状态 
	 * true 有网
	 * false 无网
	 */
	owner.getNetworkState = function() {
		var currentType = plus.networkinfo.getCurrentType()
		console.log('networkinfo currentType = ' + currentType)
		if (currentType === plus.networkinfo.CONNECTION_NONE || currentType === plus.networkinfo.CONNECTION_UNKNOW) {
			return false
		}
		return true
	}

	/**
	 * 隐藏软键盘
	 */
	owner.hideSoftKeybord = function() {
		plus.key.hideSoftKeybord();
	}

	owner.openPermissionSettings = function() {
		if (plus.os.name == "iOS") {
			var UIApplication = plus.ios.import("UIApplication");
			var application2 = UIApplication.sharedApplication();
			var NSURL2 = plus.ios.import("NSURL");
			var setting2 = NSURL2.URLWithString("app-settings:");
			application2.openURL(setting2);

			plus.ios.deleteObject(setting2);
			plus.ios.deleteObject(NSURL2);
			plus.ios.deleteObject(application2);
		} else if (plus.os.name == "Android") {
			var Intent = plus.android.importClass("android.content.Intent");
			var Settings = plus.android.importClass("android.provider.Settings");
			var Uri = plus.android.importClass("android.net.Uri");
			var mainActivity = plus.android.runtimeMainActivity();
			var intent = new Intent();
			intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
			var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
			intent.setData(uri);
			mainActivity.startActivity(intent);
		}
	}

	/**
	 * 判断定位权限是否开启
	 */
	owner.judgePermissionLocation = function() {
		var result = true;
		if (plus.os.name == "iOS") {
			var cllocationManger = plus.ios.import("CLLocationManager");
			var enable = cllocationManger.locationServicesEnabled();
			var status = cllocationManger.authorizationStatus();
			console.log("enable:" + enable);
			console.log("status:" + status);
			if (enable && status != 2) {
				result = true;
			} else {
				result = false;
			}
			plus.ios.deleteObject(cllocationManger);
		}
		return result;
	}

	/**
	 * 判断相机权限是否开启
	 */
	owner.judgePermissionPush = function() {
		var result = true;
		if (plus.os.name == "iOS") {
			var UIApplication = plus.ios.import("UIApplication");
			var app = UIApplication.sharedApplication();
			var enabledTypes = 0;
			if (app.currentUserNotificationSettings) {
				var settings = app.currentUserNotificationSettings();
				enabledTypes = settings.plusGetAttribute("types");
				console.log("enabledTypes1:" + enabledTypes);
				if (enabledTypes == 0) {
					result = false;
					console.log("推送权限没有开启");
				} else {
					result = true;
					console.log("已经开启推送功能!")
				}
				plus.ios.deleteObject(settings);
			} else {
				enabledTypes = app.enabledRemoteNotificationTypes();
				if (enabledTypes == 0) {
					result = false;
					console.log("推送权限没有开启!");
				} else {
					result = true;
					console.log("已经开启推送功能!")
				}
				console.log("enabledTypes2:" + enabledTypes);
			}
			plus.ios.deleteObject(app);
			plus.ios.deleteObject(UIApplication);
		}
		return result;
	}

	/**
	 * 判断相册权限是否开启
	 */
	owner.judgePermissionCamera = function() {
		var result = true;
		if (plus.os.name == "iOS") {
			var AVCaptureDevice = plus.ios.import("AVCaptureDevice");
			var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
			console.log("authStatus:" + authStatus);
			if (authStatus == 3) {
				result = true;
				console.log("相机权限已经开启");
			} else {
				result = false;
				console.log("相机权限没有开启");
			}
			plus.ios.deleteObject(AVCaptureDevice);
		}
		return result;
	}

	/**
	 * 相册权限是否开启
	 */
	owner.judgePermissionPhotoLibrary = function() {
		var result = true;
		if (plus.os.name == "iOS") {

			var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
			var authStatus = PHPhotoLibrary.authorizationStatus();
			console.log("authStatus:" + authStatus);
			if (authStatus == 3) {
				result = true;
				console.log("相册权限已经开启");
			} else {
				result = false;
				console.log("相册权限没有开启");
			}
			plus.ios.deleteObject(PHPhotoLibrary);
			return result;
		}
		return result;
	}

}(mui, window.app = {}))

function startUpdatingLocation() {
	if (plus.os.name == 'iOS') {
		try{
			var AMapTool = plus.ios.importClass("AMapTool");
			var amap = new AMapTool();
			amap.startUpdatingLocation();
		}catch(e){

		}
	}
}

function stopUpdatingLocation() {
	if (plus.os.name == 'iOS') {
		try{
			var AMapTool = plus.ios.importClass("AMapTool");
			var amap = new AMapTool();
			amap.stopUpdatingLocation();
		}catch(e){

		}
	}
}

